    /*******************************************************************************
         *
         *    Copyright (c) Baina Info Tech Co. Ltd
         *
         *    Timer TurnOff
         *    
         *    BluetoothControl16
         *    TODO File description or class description.
         *
         *    @author: zuochen
         *    @since:  2011-10-9
         *    @version: 1.0
         *
         ******************************************************************************/

package com.sdk.hardware.manager;

import java.lang.reflect.Method;

import android.content.Context;

/**
 * BluetoothControl16 of Timer TurnOff.
 * @author zuochen
 *
 */

public class BluetoothControl16 implements BluetoothControl{

	public static int BLUETOOTH_STATE_UNKNOWN;
	public static int BLUETOOTH_STATE_OFF;
	public static int BLUETOOTH_STATE_TURNING_ON;
	public static int BLUETOOTH_STATE_ON;
	public static int BLUETOOTH_STATE_TURNING_OFF;
	public static String BLUETOOTH_ACTION_STATE_CHANGED;
	public static String BLUETOOTH_EXTRA_STATE;
	
	private final Object mService;
	private final Method[] mMethods = new Method[3]; // [0] enable, [1]
														// disable, [2]
														// getBluetoothState

	public BluetoothControl16(Context context) throws Exception {
		mService = context.getSystemService("bluetooth"); // bluetooth
		final Method[] methods = mMethods;

		if (mService == null)
			throw new IllegalStateException("bluetooth service not found");
		Method method;
		// get enabled
		method = mService.getClass().getMethod("enable");
		if (method != null)
			method.setAccessible(true);
		methods[0] = method;

		// get disabled
		method = mService.getClass().getMethod("disable");
		if (method != null)
			method.setAccessible(true);
		methods[1] = method;

		method = mService.getClass().getMethod("getBluetoothState");
		if (method != null)
			method.setAccessible(true);
		methods[2] = method;

		// initialize state
		BLUETOOTH_STATE_UNKNOWN = -1;
		BLUETOOTH_STATE_OFF = 0;
		BLUETOOTH_STATE_TURNING_ON = 1;
		BLUETOOTH_STATE_ON = 2;
		BLUETOOTH_STATE_TURNING_OFF = 3;

		BLUETOOTH_ACTION_STATE_CHANGED = "android.bluetooth.intent.action.BLUETOOTH_STATE_CHANGED";
		BLUETOOTH_EXTRA_STATE = "android.bluetooth.intent.BLUETOOTH_STATE";
	}

	@Override
	public void setEnabled(boolean enabled) {
		try {
			final Method method = mMethods[enabled ? 0 : 1];
			method.invoke(mService);
			return;
		} catch (final Exception e) {
		}
		return;
	}

	@Override
	public int getBluetoothState() {
		try {
			final Method method = mMethods[2];
			final Integer state = (Integer) method.invoke(mService);
			return state.intValue();
		} catch (final Exception e) {
		}
		return BLUETOOTH_STATE_UNKNOWN;
	}

}
